#include "common.h"

class UdpClient
{
  public:
    UdpClient(const string& serverIp, const unsigned short serverPort)
      :_sockfd(-1)
      ,_serverPort(serverPort)
      ,_serverIp(serverIp)
    {}

    ~UdpClient()
    {
      if(_sockfd >= 0)
      {
        close(_sockfd);
      }
    }

    void InitClient()
    {
      if((_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
      {
        cerr << "socket error" << endl;
        return;
      }
    }

    void StartClient()
    {
      string msg;
      struct sockaddr_in peer;
      memset(&peer, 0, sizeof(peer));
      peer.sin_family = AF_INET;
      peer.sin_port = htons(_serverPort);
      peer.sin_addr.s_addr = inet_addr(_serverIp.c_str());

      while(true)
      {
        cout << "Please Enter# ";
        getline(cin, msg);
        if(sendto(_sockfd, msg.c_str(), SIZE-1, 0, (struct sockaddr*)&peer, sizeof(peer)) == -1)
        {
          cerr << "sendto error" << endl;
        }
      }
    }

  private:
    unsigned short _sockfd;
    unsigned short _serverPort;
    string _serverIp;
};
